<?php
namespace Mouta\UserBundle\Model;

use FOS\UserBundle\Doctrine\UserManager;
use FOS\UserBundle\Model\UserInterface;
use Mouta\AmistateBundle\Entity\Usuario;
use Mouta\UserBundle\Solr\SolrManager;
use Symfony\Component\HttpFoundation\Request;


class AmistateUserManager extends UserManager
{
    protected $_Container;

	protected $_SolrManager;
    /**
     * {@inheritDoc}
     */
    public function findUserBy(array $criteria)
    {
        return $this->repository->findCompleteUserBy($criteria);
    }

    /**
     * {@inheritDoc}
     */
    public function createUser()
    {
        $user = parent::createUser();
        $user->setIp($this->_Container->get('request')->getClientIp());
        return $user;
    }
	
	/**
     * {@inheritDoc}
     */
    public function updateUser(UserInterface $user, $andFlush = true)
    {
		//$user->setSlug(null);
        $this->updateTags($user);
		
		parent::updateUser($user, $andFlush);
		
		$this->_SolrManager->checkAndProcessUser($user);
    }


	
	public function updateTags(Usuario $user)
	{
		$om = $this->objectManager;
        $repository = $om->getRepository('AmistateBundle:Tags');
		$texto = $user->getTextoParaBuscarTags();
		$user->getTags()->clear();
		$tags = $repository->findTagsInText($texto);
		foreach($tags as $tag){
            $user->addTag($tag);
        }
	}
	
	public function setSolrManager(SolrManager $solrManager)
	{
		$this->_SolrManager = $solrManager;
	}

    public function setContainer(  $container)
    {
        $this->_Container = $container;
    }

}
